/*
 * @Description:
 * @Author: 张文韬
 * @Date: 2024-06-28 15:15:40
 * @LastEditTime: 2024-11-08 10:23:19
 * @LastEditors: 张文韬
 * @Reference: 预约模块输入项配置
 */
const defaultSpan = 4;

export function toSelectOptions(options) {
	return options.map(([label, value]) => ({ label, value }));
}

const setSpan = (option, span) => {
	option.span = span;
	return this;
};

const defaultOption = { span: defaultSpan, props: { clearable: true } };

// createInput -------------------------------------------------------------------
/**
 * @description: createInput
 * @return {*}
 */
export const createInput = (label = '', key = '', configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		// label: '',
		type: 'input',
		key,
		...configs,
		props: {
			...defaultOption.props,
			placeholder: `${label}`,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setOnBlur(e) {
			option.onChange = e;
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		option() {
			return option;
		},
	};
};

/**
 * @description: createInput
 * @return {*}
 */
export const createNumberInput = (label = '', key = '', configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		// label: '',
		type: 'numberInput',
		key,
		...configs,
		props: {
			...defaultOption.props,
			placeholder: `请输入${label}`,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setOnBlur(e) {
			option.onChange = e;
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		option() {
			return option;
		},
	};
};
// createSelect -------------------------------------------------------------------

export const createSelect = (label = '', key = '', options = [], configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		// label: '',
		type: 'select',
		key,
		options,
		// onChange: 'channel-change',
		...configs,
		props: {
			...defaultOption.props,
			placeholder: `${label}`,
			filterable: true,
			...configs.props,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		setChanged(e) {
			option.onChange = e;
			return this;
		},
		setOptions(e) {
			option.options = e;
			return this;
		},
		setOptionYN(y = '1', n = '0') {
			option.options = toSelectOptions([
				['是', y],
				['否', n],
			]);
			return this;
		},
		option() {
			return option;
		},
	};
};

// createDateSelect
export const createDateSelect = (label = '', key = '', configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		type: 'date',
		key,
		...configs,
		props: {
			...defaultOption.props,
			placeholder: `请选择${label}`,
			type: 'daterange',
			valueFormat: 'YYYY-MM-DD',
			rangeSeparator: '-',
			startPlaceholder: '开始日期',
			endPlaceholder: '结束日期',
			...configs.props,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		setChanged(e) {
			option.onChange = e;
			return this;
		},
		option() {
			return option;
		},
	};
};

export const createTimeSelect = (label = '', key = '', configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		type: 'time',
		key,
		...configs,
		props: {
			...defaultOption.props,
			placeholder: `请选择${label}`,
			valueFormat: 'yyyy-MM-dd',
			...configs.props,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		setChanged(e) {
			option.onChange = e;
			return this;
		},
		option() {
			return option;
		},
	};
};

export const createReadonly = (label = '', key = '', configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		type: 'input',
		key,
		readonly: true,
		...configs,
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		option() {
			return option;
		},
	};
};

export const createSlot = (label = '', slotName = '', configs = {}) => {
	const option = {
		label: `${label} :`,
		slotName,
		...configs,
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		option() {
			return option;
		},
	};
};

export const createCascader = (label = '', key = '', configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		// label: '',
		type: 'cascader',
		key,
		...configs,
		props: {
			...defaultOption.props,
			placeholder: `请选择${label}`,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setOptions(e) {
			option.options = e;
			return this;
		},
		setCascaderProps(e) {
			option.props = e;
			return this;
		},
		setChanged(e) {
			option.onChange = e;
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		option() {
			return option;
		},
	};
};

export const createCheckbox = (label = '', key = '', options = [], configs = {}) => {
	const option = {
		...defaultOption,
		label: `${label} :`,
		// label: '',
		type: 'checkbox',
		key,
		options,
		// onChange: 'channel-change',
		...configs,
		props: {
			...defaultOption.props,
			...configs.props,
		},
	};
	return {
		setSpan(span) {
			setSpan(option, span);
			return this;
		},
		setProps(props) {
			option.props = { ...option.props, ...props };
			return this;
		},
		setChanged(e) {
			option.onChange = e;
			return this;
		},
		setOptions(e) {
			option.options = e;
			return this;
		},
		option() {
			return option;
		},
	};
};

export default { createInput, createSelect, createDateSelect, createReadonly, createSlot, createCheckbox, createCascader };
